<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Login extends Controller
{
		//登录
    public function index()
    {
    	if($this->request->isPost()){
    		$username = $this->request->param('accountNo');
    		$password = $this->request->param('password');
            if (cookie('time') && (time()-cookie('time')<600)) {
                return ['code'=>0,'msg'=>'账号密码错误过多，请稍后登录'];
            }
    		if(!$username || !$password){
    			return ['code'=>0,'msg'=>'账号或者密码不能为空'];
    		}
    		$user = Db::name('user')->where('username',$username)->where('password',md5($password))->find();
    		if(!$user){
                if (empty(cookie('num'))) {
                    cookie('num',1);
                }else{
                    cookie('num',cookie('num')+1);
                }
                if (cookie('num')>=5) {
                    cookie('time',time());
                }
    			return ['code'=>0,'msg'=>'密码错误或者账号不存在'];
    		}
    		if($user['status'] == 0){
    			return ['code'=>0,'msg'=>'该用户已被禁用'];
    		}
            if ($user['group_id']!=2) {
                return ['code'=>0,'msg'=>'请用打手账号登录'];
            }
            $user['outtime'] = time();
    		session('userinfo',$user);
            Db::name('user')->where('id',$user['id'])->update(['last_time'=>time()]);
            Db('login_log')->insert(['uid'=>$user['id'],'create_time'=>time(),'ip'=>$this->request->ip()]);
            cookie('time',null);
    		return ['code'=>1,'msg'=>'账号密码正确','group_id'=>$user['group_id']];
    	}else{
    		return $this->fetch();
    	}    	
    }
    //退出
    public function logout(){
        $group_id = session('userinfo.group_id');
    	session('userinfo',null);
        if ($group_id==2) {
            $this->redirect('/index/login');
        }else{
            $this->redirect('/index/login/adminlogin');
        }
    }

    public function adminlogin()
    {
        if($this->request->isPost()){
            $username = $this->request->param('accountNo');
            $password = $this->request->param('password');
            if (cookie('time') && (time()-cookie('time')<600)) {
                return ['code'=>0,'msg'=>'账号密码错误过多，请稍后登录'];
            }
            if(!$username || !$password){
                return ['code'=>0,'msg'=>'账号或者密码不能为空'];
            }
            $user = Db::name('user')->where('username',$username)->where('password',md5($password))->find();
            if(!$user){
                if (empty(cookie('num'))) {
                    cookie('num',1);
                }else{
                    cookie('num',cookie('num')+1);
                }
                if (cookie('num')>=5) {
                    cookie('time',time());
                }
                return ['code'=>0,'msg'=>'密码错误或者账号不存在'];
            }
            if($user['status'] == 0){
                return ['code'=>0,'msg'=>'该用户已被禁用'];
            }
            $user['outtime'] = time();
            session('userinfo',$user);
            Db::name('user')->where('id',$user['id'])->update(['last_time'=>time()]);
            Db('login_log')->insert(['uid'=>$user['id'],'create_time'=>time(),'ip'=>$this->request->ip()]);
            cookie('time',null);
            return ['code'=>1,'msg'=>'账号密码正确','group_id'=>$user['group_id']];
        }else{
            return $this->fetch();
        } 
    }
}
